package io.renren.modules.task.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.HttpClientUtil;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.task.constant.ManagePublicMethod;
import io.renren.modules.task.entity.MtCcAgvCommunLogEntity;
import io.renren.modules.task.entity.MtCcEquipmentRunInfoEntity;
import io.renren.modules.task.entity.MtCcEquipmentRunInfoLogEntity;
import io.renren.modules.task.enums.MtCcAGVEnum;
import io.renren.modules.task.enums.MtCcEnum;
import io.renren.modules.task.model.AgvParam;
import io.renren.modules.task.model.MtCcAreaRouteEntityVo;
import io.renren.modules.task.model.Position;
import io.renren.modules.task.service.MtCcAgvCommunLogService;
import io.renren.modules.task.service.MtCcAreaRouteService;
import io.renren.modules.task.service.MtCcEquipmentRunInfoLogService;
import io.renren.modules.task.service.MtCcEquipmentRunInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

import static io.renren.modules.task.constant.ManageConstant.EMPTY_CAR_RETURN_ORDER_TYPE;

/**
 * @Author: weihongyang
 * @Date: 2019/4/30 15:41
 * @Version 1.0
 */
@RestController
@Api(description = "空车回库操作API")
@RequestMapping("empty/car/return")
public class EmptyCarReturnController extends AbstractController {


    @Autowired
    private MtCcAreaRouteService mtCcAreaRouteService;

    @Autowired
    private MtCcEquipmentRunInfoLogService mtCcEquipmentRunInfoLogService;

    @Autowired
    private MtCcAgvCommunLogService mtCcAgvCommunLogService;

    @Autowired
    private MtCcEquipmentRunInfoService mtCcEquipmentRunInfoService;

    @PostMapping("/to/agv")
    @ApiOperation(value = "空车回库操作", notes = "空车回库操作")
    public R selectEmptyPointToAgv() throws Exception {
        String url = MtCcAGVEnum.AGVURL_emptyCar.getCode();
        //根据操作台号来获取空车出库的路线
        System.out.println("该用户的操作台号：" + getUser().getConsoleCode());
        String c = getUser().getConsoleCode();
        MtCcAreaRouteEntityVo mtCcAreaRouteEntityVo =
                mtCcAreaRouteService.selectEmptyCarPoint(getUser().getConsoleCode());
        Integer count = mtCcEquipmentRunInfoLogService.selectCountByTaskType(2) + 1;
        System.out.println("总数是" + count);
        String reqCode = ManagePublicMethod.creatOrderCode(EMPTY_CAR_RETURN_ORDER_TYPE,
                String.valueOf(getUser().getConsoleCode()), String.valueOf(count));
        String taskTyp = "KCHK";
//        Position[] positions = new Position[2];
        Position position = new Position();
        String num = mtCcAreaRouteEntityVo.getSourceNo();
        position.setPositionCode(num);
//        position.setPositionCode("204835AA203655");
        position.setType("00");
        Position position1 = new Position();
        position1.setType("00");
        position1.setPositionCode(mtCcAreaRouteEntityVo.getDestinationNo());
//        position1.setPositionCode("282655AA228370");
        Object[] positionCodePath = {position, position1};
        AgvParam agvParam = new AgvParam();
        agvParam.setReqCode(reqCode);
        agvParam.setTaskCode(reqCode);
        agvParam.setAgvCode("3002");
        agvParam.setTaskTyp(taskTyp);
        agvParam.setPositionCodePath(positionCodePath);
        String resultMsg = null;//获取服务端返回的参数
        String param = JSONObject.toJSONString(agvParam);//将对象转换为JSON格式
        MtCcAgvCommunLogEntity mtCcAgvCommunLogEntity = new MtCcAgvCommunLogEntity();
        Date date = new Date();
        mtCcAgvCommunLogEntity.setCreateTime(date);
        mtCcAgvCommunLogEntity.setEquipmentId(getUser().getConsoleCode());
        mtCcAgvCommunLogEntity.setStatus(0);
        mtCcAgvCommunLogEntity.setTaskCode(reqCode);
        mtCcAgvCommunLogEntity.setContent("agv日志已生成");
        mtCcAgvCommunLogService.save(mtCcAgvCommunLogEntity);

        MtCcEquipmentRunInfoEntity mtCcEquipmentRunInfoEntity = new MtCcEquipmentRunInfoEntity();
        mtCcEquipmentRunInfoEntity.setCreateTime(date);
        mtCcEquipmentRunInfoEntity.setEquipmentId(getUser().getConsoleCode());
        mtCcEquipmentRunInfoEntity.setUserId(getUser().getUserId().toString());
        mtCcEquipmentRunInfoEntity.setTaskName(MtCcEnum.EMPTYCARWAREHOUSING.getMsg());
        mtCcEquipmentRunInfoEntity.setTaskType(MtCcEnum.EMPTYCARWAREHOUSING.getCode());
        mtCcEquipmentRunInfoEntity.setTaskCode(reqCode);
        mtCcEquipmentRunInfoEntity.setEnable(0);
        mtCcEquipmentRunInfoEntity.setStatus(0);
        mtCcEquipmentRunInfoService.save(mtCcEquipmentRunInfoEntity);

        MtCcEquipmentRunInfoLogEntity mtCcEquipmentRunInfoLogEntity = new MtCcEquipmentRunInfoLogEntity();
        mtCcEquipmentRunInfoLogEntity.setCreateTime(date);
        mtCcEquipmentRunInfoLogEntity.setEquipmentId(getUser().getConsoleCode());
        mtCcEquipmentRunInfoLogEntity.setUserId(getUserId().toString());
        mtCcEquipmentRunInfoLogEntity.setTaskName(MtCcEnum.EMPTYCARWAREHOUSING.getMsg());
        mtCcEquipmentRunInfoLogEntity.setTaskType(MtCcEnum.EMPTYCARWAREHOUSING.getCode());
        mtCcEquipmentRunInfoLogEntity.setTaskCode(reqCode);
        mtCcEquipmentRunInfoLogEntity.setEnable(0);
        mtCcEquipmentRunInfoLogEntity.setStatus(1);
        mtCcEquipmentRunInfoLogService.save(mtCcEquipmentRunInfoLogEntity);

        resultMsg = HttpClientUtil.post(url, param);// 以post形式调用webservice

        JSONObject jsonObject = JSON.parseObject(resultMsg);
        String code = jsonObject.getString("code");
        if (code.equals("0")) {
            mtCcAgvCommunLogEntity.setStatus(1);
            mtCcAgvCommunLogEntity.setContent("agv日志已生成,调用agv接口成功");
            mtCcAgvCommunLogService.update(mtCcAgvCommunLogEntity, new QueryWrapper<MtCcAgvCommunLogEntity>().eq("task_code",
                    reqCode));
            return R.ok("成功");
        } else if (code.equals("1")) {
            return R.error("参数相关的错误");
        } else {
            return R.error("其他未知错误");
        }
    }
}
